<?php
/**
 * 切换微信服务状态
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);
header('Content-Type: application/json');

// 获取POST数据
$input = file_get_contents('php://input');
$data = json_decode($input, true);

if (!isset($data['enabled'])) {
    echo json_encode(['success' => false, 'message' => '缺少必要参数']);
    exit;
}

// 读取现有配置
$configFile = __DIR__ . '/../../config.php';
$config = include $configFile;

// 更新状态
if (!isset($config['plugins'])) {
    $config['plugins'] = [];
}

if (!isset($config['plugins']['wechat'])) {
    $config['plugins']['wechat'] = [
        'enabled' => false,
        'app_id' => '',
        'app_secret' => '',
        'redirect_uri' => ''
    ];
}

$config['plugins']['wechat']['enabled'] = (bool)$data['enabled'];

// 写入配置文件
$configContent = "<?php\n/**\n * 氚云API管理系统配置文件\n * Alyr.net 企业级配置管理\n */\n\nreturn " . var_export($config, true) . ";\n";

if (file_put_contents($configFile, $configContent)) {
    echo json_encode(['success' => true, 'message' => '状态更新成功']);
} else {
    echo json_encode(['success' => false, 'message' => '配置文件写入失败']);
}
